<template>
	<view class="content bg_com">
		<view class="header_warp">
			<image class="header_bg" src="../../../static/my_bg.png" mode=""></image>
			<view class="user_info row_center">
				<view class="tx_box" @click="jumpHomePage">
					<image v-if="user.avatar" class="tx_img" :src="user.avatar" mode=""></image>
					<image v-else class="tx_img" src="../../../static/logo.jpg" mode=""></image>
				</view>
				<view class="name_id">
					<view class="name row_center">
						<text v-if="hasLogin">{{user.nick}}</text>
						<text v-else @click="jumpHomePage">请先注册/登录</text>
						<image v-if="user.gender == 1" class="img_val" src="../../../static/man.png" mode=""></image>
						<image v-else-if="user.gender == 2" class="img_val" src="../../../static/women.png" mode="">
						</image>
					</view>
					<view v-if="hasLogin" class="user_id row_center">
						<image class="img_val" src="../../../static/id2x.png" mode=""></image>
						<text class="id_val">{{user.erbanNo}}</text>
						<view class="copy_btn" @click="copyFun">
							复制
						</view>
					</view>
				</view>
				<view class="btn" @click="jumpProfile">
					<i class="iconfont icon-arrow-right"></i>
				</view>
			</view>
			<view class="sign">
				<image class="yh" src="../../../static/yinhao.png" mode=""></image>
				<view class="sign_txt" v-if="hasLogin">
					{{user.userDesc || '说点什么吧'}}
				</view>
				<view class="sign_txt" v-else>
					这条个性签名是32个字符显示区域呢
				</view>
			</view>
		</view>
		<view class="info_show row_center">
			<!-- <view class="_item">
				<view class="val_txt">
					{{user.fansNum || 0}}
				</view>
				<view class="name">
					好友
				</view>
			</view> -->
			<view class="_item">
				<view class="val_txt">
					{{user.followNum || 0}}
				</view>
				<view class="name">
					关注
				</view>
			</view>
			<view class="_item">
				<view class="val_txt">
					{{user.fansNum || 0}}
				</view>
				<view class="name">
					粉丝
				</view>
			</view>
		</view>
		<!-- 菜单 -->
		<view class="menu_list">
			<view class="_item row_center" @click="jumpPage(item)" v-for="(item, index) in menulist" :key="index">
				<view class="left row_center">
					<view class="icon">
						<image :src="item.iconfont" mode="aspectFill" class="img_val"></image>
					</view>
					<view class="name">
						{{item.name}}
					</view>
				</view>
				<view class="right">
					<i v-if="item.right" class="iconfont icon-arrow-right"></i>
				</view>
			</view>
		</view>
		<!-- 邀请码 -->
		<com-code @confirm="profileConfirm" :maxlen="32" :title="popTitle" ref="modify" />
	</view>
</template>

<script>
	import {
		mapActions,
		mapState
	} from 'vuex';
	export default {
		computed: mapState(['hasLogin', 'user', 'identity']),
		data() {
			return {
				popType: 2,
				popTitle: '填写邀请码',
				menulist: [{
					type: 'notPath',
					name: '填写邀请码',
					iconfont: '../../../static/my/my_yqm.png',
					path: '',
					right: false
				}, {
					name: '我的钱包',
					iconfont: '../../../static/my/my_qb.png',
					path: '../../wallet/wallet',
					right: true
				}, {
					name: '实名认证',
					iconfont: '../../../static/my/my_smrz.png',
					path: '/pages/youthMode/forgetPassword?title=实名认证',
					right: true
				}, {
					name: '我的等级',
					iconfont: '../../../static/my/my_dj.png',
					path: `${this.$urlConfig.h5_config}wanpi/app/myGrade`,// 暂定为开发环境 生产环境需要替换
					type: 'goWeb',
					right: true
				}, {
					name: '设置',
					iconfont: '../../../static/my/m_sz.png',
					path: '../../setting/setting',
					right: true
				}]

			}
		},
		onLoad() {
			if(this.user.emptyChannel) {
				this.open();
			}
			this.getGrade();
		},
		onPullDownRefresh() {
			console.log('refresh');
			setTimeout(() => {
				this.getUserInfo();
			}, 500);
		},
		methods: {
			...mapActions(['GetInfo']),
			/* 获取等级 */
			getGrade() {
				if(process.env.NODE_ENV === 'development'){
				    console.log('开发环境')
				}else{
				    console.log('生产环境')
				}
			},
			getUserInfo() {
				this.GetInfo({
					uid: this.identity.uid
				}).then(res => {
					uni.stopPullDownRefresh();
				}).catch(err => {
					uni.stopPullDownRefresh();
				});
			},
			copyFun() {
				uni.setClipboardData({
					data: this.user.erbanNo + '',
					success: function() {
						uni.showToast({
							title: '复制成功',
							icon: 'none'
						})
					},
					fail(err) {
						console.log(err)
						uni.showToast({
							title: '复制失败',
							icon: 'none'
						})
					}
				});
			},
			jumpProfile() {
				if (!this.hasLogin) {
					uni.navigateTo({
						url: '../../login/login'
					})
					return false;
				}
				uni.navigateTo({
					url: '../../profile/profile'
				})
			},
			jumpPage(item) {
				if (!this.hasLogin) {
					uni.navigateTo({
						url: '../../login/login'
					})
					console.log("请先登录")
					return false;
				}
				if (item.type && item.type == 'notPath') {
					this.open();
					return;
				}
				if(item.type && item.type == 'goWeb') {
					uni.navigateTo({
						url: `/pages/activeWebView/activeWebView?vurl=${item.path}&uid=${this.identity.uid}`
					})
				}
				uni.navigateTo({
					url: item.path
				})
			},
			/* 跳转个人主页 */
			jumpHomePage() {
				if (!this.hasLogin) {
					uni.navigateTo({
						url: '../../login/login'
					})
					console.log("请先登录")
					return false;
				}
				uni.navigateTo({
					url: '../../homePage/homePage'
				})
			},
			open(index) {
				this.popType = 4;
				this.$refs.modify.open();
			},
			/* 填写邀请码 */
			addInviterFun(channelFlag) {
				this.$api.addInviter({
					uid: this.identity.uid,
					channelFlag
				}).then(res => {
					if(res.code == 200) {
						uni.showToast({
							title: '填写成功'
						})
						this.$refs.modify.close();
					}
				})
			},
			/* 邀请码弹窗输入弹窗点击了确认 */
			profileConfirm(e) {
				console.log(e);
				if(e.value == '') {
					uni.showToast({
						title: '邀请码不可为空',
						icon: 'none'
					})
					return;
				}
				this.addInviterFun(e.value);
			},
		}
	}
</script>

<style lang="scss">
	@import "@/static/iconfont/iconfont.css";

	@font-face {
		font-family: 'iconfont';
		src: url('~@/static/iconfont/iconfont.ttf');
	}

	@import './index.scss';
</style>
